<template>
  <div>

    <el-carousel :interval="4000" type="card" height="200px">
      <el-carousel-item v-for="item in itemList" :key="item">
        <h3 class="small">{{ item }}</h3>
      </el-carousel-item>
    </el-carousel>

    <!-- 结果列表 -->
    <el-card>
      <el-table :data="purchaseList" stripe style="width: 100%">
        <el-table-column prop="id" label="#" width="80">
        </el-table-column>
        <el-table-column prop="type" label="充值类型" width="180"/>
        <el-table-column prop="inventory" label="库存" width="280"/>
        <el-table-column prop="price" label="价格" width="280"/>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button type="success" round @click="purchase(scope.row)" >
            购买
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
// eslint-disable-next-line no-unused-vars
import tb from '@/api/tb'

export default {
  data() {
    return {
      itemList: ['小毛驴电商自学一条龙 解锁全部课程需要4000点（200元可以兑换5000点）',
        '卡密联系客服 10元100点 25元400点 50元1000点 200元5000点',
        '1次起3个店铺 2个月内做9家店铺 2个月内不回本 课程费全退'],
      formLabelWidth: '130px',
      dialogFormVisible: false,
      title: '',
      total: 0,
      purchaseList: []
    }
  },
  created() {
    this.getPurchaseList()
  },
  methods: {
    getPurchaseList() {
      tb.getPurchaseList().then((response) => {
        this.purchaseList = response.data.rows
        console.log('2222' + response.data)
      })
    },
    purchase(purchaseInfo) {
      tb.purchase(purchaseInfo).then(res => {
        // 成功提示
        if (res.code === 20000) {
          // 添加之前先删除一下，如果单页面，页面不刷新，添加进去的内容会一直保留在页面中，二次调用form表单会出错
          const divForm = document.getElementsByTagName('divform')
          if (divForm.length) {
            document.body.removeChild(divForm[0])
          }
          const div = document.createElement('divform')
          div.innerHTML = res.message // data就是接口返回的form 表单字符串
          document.body.appendChild(div)
          document.forms[0].setAttribute('target', '_blank') // 新开窗口跳转
          document.forms[0].submit()
        } else {
          this.$message({
            message: res.message,
            type: 'error'
          })
        }
      })
    }
  }
}
</script>

<style>
#search .el-input {
  width: 200px;
  margin-right: 10px;
}

.el-dialog .el-input {
  width: 85%;
}

.el-carousel__item h3 {
  color: #475669;
  font-size: 20px;
  opacity: 0.75;
  line-height: 200px;
  margin: 0;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n+1) {
  background-color: #d3dce6;
}
</style>
